package com.spzx.product;

import com.spzx.common.security.annotation.EnableCustomConfig;
import com.spzx.common.security.annotation.EnableRyFeignClients;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.service.ProductSkuService;
import jakarta.annotation.Resource;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.List;

@SpringBootApplication
@EnableCustomConfig
@EnableRyFeignClients
public class SpzxProductApplication implements InitializingBean {

    public static void main(String[] args) {
        SpringApplication.run(SpzxProductApplication.class , args);
    }

    @Resource
    ProductSkuService productSkuService;
    @Resource
    RedissonClient redissonClient;

    //当前项目启动时，初始化布隆过滤器 并存入商品详情的 skuid
    @Override
    public void afterPropertiesSet() throws Exception {
        //初始化布隆过滤器
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter("spzx:channel:sku:bloom");
        bloomFilter.tryInit(100000 , 0.01);
        //查询skuid集合  如果skuId存在  存入到bloom过滤器中
        List<ProductSku> skuList =
                productSkuService.list();
        skuList.forEach(sku->{
            bloomFilter.add(sku.getId());
        });

    }

}
